package com.jy.rhin.support.interceptor.convertor;

import cn.org.chiss.rhin._2015.document.repository.ProvideAndRegisterDocumentSet;
import cn.org.chiss.rhin._2015.document.repository.ProvideAndRegisterDocumentSetResponse;
import com.jy.rhin.application.enums.*;
import com.jy.rhin.sdk.dto.audit.AuditSourceDto;
import com.jy.rhin.sdk.dto.audit.CodedValueTypeDto;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @Description: 文档提交
 * @Author: lww
 * @Date: 2022/5/31 14:31
 */
@Service("AuditInfoConverter4TerminologyProviderAndRegistryDocumentSet")
public class AuditInfoConverter4TerminologyProviderAndRegistryDocumentSet extends BaseAuditInfoConverter<ProvideAndRegisterDocumentSet, ProvideAndRegisterDocumentSetResponse> {

    public AuditInfoConverter4TerminologyProviderAndRegistryDocumentSet() {
        super(EventActionCodeEnum.CREATE,
                ServiceCodeEnum.DOCUMENTS_STORAGE,
                EventTypeCodeEnum.PROVIDER_AND_REGISTRY_DOCUMENT,
                true,
                ParticipantObjectIDTypeCodeEnum.PATIENT_NUMBER,
                ParticipantObjectTypeCodeRoleEnum.MASTER_FILE,
                ParticipantObjectTypeCodeEnum.SYSTEM_OBJECT,
                ParticipantObjectDataLifeCycleEnum.ORIGINATION_CREATION);
    }

    /**
     * 审计源（审计源为事件目标）
     *
     * @return
     */
    @Override
    List<AuditSourceDto> buildAuditSource() {
        AuditSourceDto auditSource = new AuditSourceDto();
        // TODO 从安全节点验证服务，获取节点OID
        auditSource.setBiaoShi("documents-storage-service");
        // TODO: 从SOAP报文头获取<TO>节点，做为审计源网址
        auditSource.setWangZhi("http://localhost");
        CodedValueTypeDto auditSourceTypeCode = new CodedValueTypeDto();
        auditSourceTypeCode.setCodeSystem(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getCode());
        auditSourceTypeCode.setCodeSystemName(CodeSystemEnum.CV_AUDIT_SOURCE_TYPE_CODE.getName());
        auditSourceTypeCode.setCode(AuditSourceTypeCodeEnum.WEB_SERVER_PROCESS.getCode());
        auditSourceTypeCode.setDisplayName(ServiceCodeEnum.DOCUMENTS_STORAGE.getName());
        auditSource.setLeiXing(auditSourceTypeCode);
        return Collections.singletonList(auditSource);
    }

    @Override
    AuditParticipantUserDesc buildSourceUser(ProvideAndRegisterDocumentSet provideAndRegisterDocumentSet, ProvideAndRegisterDocumentSetResponse provideAndRegisterDocumentSetResponse) {
        return new AuditParticipantUserDesc("client-2", "客户端");
    }

    @Override
    AuditParticipantUserDesc buildOriginatorUser(ProvideAndRegisterDocumentSet provideAndRegisterDocumentSet, ProvideAndRegisterDocumentSetResponse provideAndRegisterDocumentSetResponse) {
        return new AuditParticipantUserDesc("system", "admin");
    }

    @Override
    AuditParticipantUserDesc buildTargetUser(ProvideAndRegisterDocumentSet provideAndRegisterDocumentSet, ProvideAndRegisterDocumentSetResponse provideAndRegisterDocumentSetResponse) {
        return new AuditParticipantUserDesc("documents-storage-service", ServiceCodeEnum.DOCUMENTS_STORAGE.getName());
    }

    @Override
    String getObjectId(ProvideAndRegisterDocumentSet provideAndRegisterDocumentSet, ProvideAndRegisterDocumentSetResponse provideAndRegisterDocumentSetResponse) {
        if (Objects.nonNull(provideAndRegisterDocumentSet)
                && Objects.nonNull(provideAndRegisterDocumentSet.getSubmissionSet())
                && Objects.nonNull(provideAndRegisterDocumentSet.getSubmissionSet().getUniqueId())) {
            return provideAndRegisterDocumentSet.getSubmissionSet().getUniqueId().getValue();
        }
        if (Objects.nonNull(provideAndRegisterDocumentSetResponse)
                && Objects.nonNull(provideAndRegisterDocumentSetResponse.getSubmissionSetUUID())) {
            return provideAndRegisterDocumentSetResponse.getSubmissionSetUUID().getValue();
        }
        return "";
    }
    
    @Override
	public boolean isMatch(String method, Object data) {
		return "DocumentStoragePortImpl.providerAndRegistryDocumentSet".equals(method);
	}
}
